package com.ruoyi.product.controller;

import java.util.List;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.product.common.MerchantUtils;
import com.ruoyi.product.common.RoleConst;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.product.domain.Filestore;
import com.ruoyi.product.service.IFilestoreService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品资源Controller
 *
 * @author ruoyi
 * @date 2021-04-25
 */
@RestController
@RequestMapping("/filestore")
public class FilestoreController extends BaseController {
    @Autowired
    private IFilestoreService filestoreService;

    /**
     * 查询商品资源列表
     */
    @PreAuthorize(hasPermi = "product:filestore:list", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @GetMapping("/list")
    public TableDataInfo list(Filestore filestore) {
        Long merchantId = MerchantUtils.getMerchantId();
        filestore.setMid(merchantId);
        startPage();
        List<Filestore> list = filestoreService.selectFilestoreList(filestore);
        return getDataTable(list);
    }

    /**
     * 导出商品资源列表
     */
    @PreAuthorize(hasPermi = "product:filestore:export", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @Log(title = "商品资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Filestore filestore) throws IOException {
        Long merchantId = MerchantUtils.getMerchantId();
        filestore.setMid(merchantId);
        List<Filestore> list = filestoreService.selectFilestoreList(filestore);
        ExcelUtil<Filestore> util = new ExcelUtil<Filestore>(Filestore.class);
        util.exportExcel(response, list, "商品资源数据");
    }

    /**
     * 获取商品资源详细信息
     */
    @PreAuthorize(hasPermi = "product:filestore:query", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Long merchantId = MerchantUtils.getMerchantId();
        Filestore param = new Filestore();
        param.setMid(merchantId);
        param.setId(id);
        List<Filestore> list = filestoreService.selectFilestoreList(param);
        if (org.springframework.util.CollectionUtils.isEmpty(list)) {
            AjaxResult.success();
        }
        return AjaxResult.success(filestoreService.selectFilestoreById(id));
    }

    /**
     * 新增商品资源
     */
    @PreAuthorize(hasPermi = "product:filestore:add", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @Log(title = "商品资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Filestore filestore) {
        Long merchantId = MerchantUtils.getMerchantId();
        filestore.setMid(merchantId);
        return toAjax(filestoreService.insertFilestore(filestore));
    }

    /**
     * 修改商品资源
     */
    @PreAuthorize(hasPermi = "product:filestore:edit", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @Log(title = "商品资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Filestore filestore) {
        Long merchantId = MerchantUtils.getMerchantId();

        Filestore param = new Filestore();
        param.setId(filestore.getId());
        param.setMid(merchantId);
        if (CollectionUtils.isEmpty(filestoreService.selectFilestoreList(param))) {
            return toAjax(0);
        }
        filestore.setMid(merchantId);
        return toAjax(filestoreService.updateFilestore(filestore));
    }

    /**
     * 删除商品资源
     */
    @PreAuthorize(hasPermi = "product:filestore:remove", hasAnyRoles = {RoleConst.MERCHANT_ROLE, RoleConst.ADMIN})
    @Log(title = "商品资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long merchantId = MerchantUtils.getMerchantId();
        Filestore param = new Filestore();
        param.setMid(merchantId);
        for (Long id : ids) {
            param.setId(id);

            if (CollectionUtils.isEmpty(filestoreService.selectFilestoreList(param))) {
                return toAjax(0);
            }
        }
        return toAjax(filestoreService.deleteFilestoreByIds(ids));
    }
}
